package com.pengtu.gsj.controller.yh;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pengtu.gsj.controller.base.DefaultBaseController;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.entity.yh.Reconstruction;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.yh.ReconstructionService;

@Controller
@RequestMapping("reconstruct")
public class ReconstructionController extends DefaultBaseController {

	@Autowired
	private ReconstructionService reconstructionService;
	
	@ModelAttribute
	public void loadEntity(String id, String type, Model model) {
		Reconstruction reconstruction = null;
		if (StringUtils.isNotEmpty(id)) {
			reconstruction = reconstructionService.getReconstruction(id);
		} else {
			reconstruction = new Reconstruction();
		}
		model.addAttribute("reconstruction", reconstruction);
		model.addAttribute("type", type);
	}
	
	@RequestMapping("listInfo")
	@Token(save = true)
	public String showLisfInfo() {
		return "yh/reconstruct_list";
	}
	
	@RequestMapping("statistics")
	@Token(save = true)
	public String showStatistics() {
		return "yh/reconstruct_statistics";
	}
	
	@RequestMapping("count")
	@ResponseBody
	public List<List<Map<String, Object>>> count(){
		List<List<Map<String, Object>>> list = new ArrayList<List<Map<String,Object>>>();
		list = reconstructionService.count();
		return list;
	}
	
	@RequestMapping(value="list", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Page<Reconstruction> getReconstructionList(Page<Reconstruction> page, HttpServletRequest request){
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("projectYear,createDate");
			page.setOrder("asc,desc");
		}
		page = reconstructionService.findReconstructionList(page, queryFilter);
		return page;
	}
	
	@RequestMapping(value = "save", method=RequestMethod.POST)
	@ResponseBody
	@Token(remove = true)
	public boolean saveReconstruction(@ModelAttribute Reconstruction reconstruction) {
		reconstructionService.save(reconstruction);
		return true;
	}
	
	@RequestMapping(value="show")
	@ResponseBody                                                     
	public Reconstruction showReconstruction (@ModelAttribute Reconstruction reconstruction){
		return reconstruction;
	}
	
	@RequestMapping(value="delete")
	@ResponseBody
	public boolean deleteReconstruction(@ModelAttribute Reconstruction reconstruction){
		reconstructionService.delete(reconstruction);
		return true;
	}
}
